package com.yun.blog.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yun.blog.bean.Album;
import com.yun.blog.bean.Result;
import com.yun.blog.service.AlbumService;
import com.yun.blog.service.PictureService;
import com.yun.blog.utils.FileUtils;
import com.yun.blog.utils.QianiuUploadUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * title        : AlbumController
 * projectName  : dantouBlog-back
 * description  : TODO
 * author       : simple
 * date         : 2022/12/1下午2:52
 */

@Slf4j
@RestController
@RequestMapping("/album")
@Api(tags = "相册接口")
public class AlbumController {
    @Resource
    AlbumService albumService;

    @Resource
    PictureService pictureService;

    @GetMapping("/getAlbums")
    @ApiOperation("获取相册列表分页中一页的数据")
    public Result getAlbums(@RequestParam(defaultValue = "10") Integer pageSize,
                                @RequestParam(defaultValue = "1") Integer pageNum) {

        Result success = Result.success();

        Page<Album> page = albumService.customPage(new Page<>(pageNum, pageSize));

        success.add("page", page);
        return success;
    }

    @GetMapping("/getAllAlbums")
    @ApiOperation("获取所有的相册")
    public Result getAllAlbums() {
        Result success = Result.success();
        List<Album> albums = albumService.list();
        success.add("albums", albums);
        return success;
    }

    @GetMapping("/getShowAlbums")
    @ApiOperation("获取开启展示的相册")
    public Result getShowAlbums() {
        Result success = Result.success();
        // 查找所有可以展示的相册
        LambdaQueryWrapper<Album> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(Album::getIsShow, true);
        List<Album> albums = albumService.list(queryWrapper);
        success.add("albums", albums);
        return success;
    }

    @ApiOperation("通过id获取相册信息")
    @GetMapping("/getAlbumById")
    public Album getAlbumById(Integer id) {
        return albumService.getByAlbumId(id);
    }

    @ApiOperation("新建相册或者保存相册修改")
    @PostMapping("/save")
    public Result save(@RequestBody Album album) {
        try {
            albumService.saveOrUpdate(album);
        } catch (Exception e) {
            log.error("保存失败：错误信息为：{}", e.getMessage());
            return Result.fail(e.getMessage());
        }
        return Result.success("保存成功！");
    }

    @GetMapping("/deleteAlbum")
    @ApiOperation("删除相册")
    public Result deleteAlbum(Integer id) {
        try {
            // 先删除相册内的图片
            pictureService.delByAlbumId(id);
            // 再删除相册
            albumService.removeById(id);
        } catch (Exception e) {
            log.error("删除失败：错误信息为：{}", e.getMessage());
            return Result.fail(e.getMessage());
        }
        return Result.success("删除成功！");
    }


    @PostMapping("/uploadImg")
    @ApiOperation("相册上传图片")
    public Result uploadImg(MultipartFile albumCover) {
        Result fail = Result.fail();
        if (albumCover == null || albumCover.isEmpty()) {
            fail.setMsg("图片不能为空！");
            return fail;
        }
        String filename = albumCover.getOriginalFilename();
        boolean allowPic = FileUtils.isAllowPic(filename);
        if (!allowPic) {
            fail.setMsg("图片类型不正确！");
            return fail;
        }

        try {
            // String imgPath = saveImg(albumCover, "albumCover");
            String imgPath = QianiuUploadUtils.upload(albumCover, "albumCover");
            Result success = Result.success("图片保存成功！");
            success.add("imgPath", imgPath);
            return success;
        } catch (Exception e) {
            log.error("图片保存失败！，错误信息为：{}", e.getMessage());
            fail.setMsg("图片保存失败！，错误信息为：" + e.getMessage());
            return fail;
        }
    }
}
